package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
    private Button btnSend;
    private MyBroadcastReceiver receiver;
    private Button btnSendLocal;
    private HighReceiver hreceiver;
    private MidReceiver mreceiver;
    private LowReceiver lreceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*        receiver =new MyBroadcastReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("weather_report");
        registerReceiver(receiver,filter);*/

//
//        mreceiver=new MidReceiver();
//        lreceiver=new LowReceiver();
        IntentFilter hfilter=new IntentFilter();
        hfilter.addAction("weather_report");
        hfilter.setPriority(100);
        hreceiver=new HighReceiver();
        registerReceiver(hreceiver,hfilter);

        IntentFilter mfilter=new IntentFilter();
        mfilter.addAction("weather_report");
        mfilter.setPriority(100);
        mreceiver=new MidReceiver();
        registerReceiver(mreceiver,mfilter);

        IntentFilter lfilter=new IntentFilter();
        lfilter.addAction("weather_report");
        lfilter.setPriority(100);
        lreceiver=new LowReceiver();
        registerReceiver(lreceiver,lfilter);


        registerLocalReceiver();


        btnSend=findViewById(R.id.btn_send);
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction("weather_report");
//    带权限的广播
//            sendBroadcast(intent,
//                        "weather_report_pemission");
//    不带权限的广播
//                sendBroadcast(intent);
//    有序广播
                sendOrderedBroadcast(intent,null);
            }
        });


        btnSendLocal=findViewById(R.id.btn_send_local);
        btnSendLocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

//    本地广播
                LocalBroadcastManager manager=
                        LocalBroadcastManager.getInstance(MainActivity.this);
                Intent intent=new Intent();
                intent.setAction("traffic_report");
                manager.sendBroadcast(intent);
            }

        });


    }
    //动态注册
    private void registerLocalReceiver() {
        receiver=new MyBroadcastReceiver();
        IntentFilter filter= new IntentFilter();
        filter.addAction("traffic_report");
        LocalBroadcastManager manager=
                LocalBroadcastManager.getInstance(MainActivity.this);
        manager.registerReceiver(receiver,filter);
    }
}
